package com.google.samples.apps.library_common.extension

import android.content.Context
import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.*
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes


/**
 * Created by Administrator on 2021/3/12 0012.
 * Describe:
 */

fun CharSequence.spanBold() = spans(StyleSpan(Typeface.BOLD))
fun CharSequence.spanItalic() = spans(StyleSpan(Typeface.ITALIC))
fun CharSequence.spanUnderline() = spans(UnderlineSpan())
fun CharSequence.spanColor(@ColorInt color: Int) = spans(ForegroundColorSpan(color))
fun CharSequence.spanBackgroundColor(@ColorInt color: Int) = spans(BackgroundColorSpan(color))
fun CharSequence.spanStrikeThrough() = spans(StrikethroughSpan())
fun CharSequence.spanScale(proportion: Float) = spans(RelativeSizeSpan(proportion))
fun CharSequence.spanSuperscript(proportion: Float) = spans(SuperscriptSpan())
fun CharSequence.spanImg(context: Context,@DrawableRes res:Int) = spans(ImageSpan(context, res))

fun CharSequence.spanSubscript(s: CharSequence) = spans(this).append(s)


fun CharSequence.spans(o: Any? = null): SpannableStringBuilder = when (this) {
    is SpannableStringBuilder -> this
    else -> SpannableStringBuilder(this)
}.apply { o?.let { setSpan(o, 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) } }

fun spanImg(context: Context,@DrawableRes res:Int) = "${res}".spans(ImageSpan(context, res))
